 //
//  InfoMainView.m
//  ejiaqingAlertView
//
//  Created by 杨攀 on 16/10/13.
//  Copyright © 2016年 CMIG. All rights reserved.
//

#import "InfoMainView.h"
#import <Masonry.h>

@interface InfoMainView ()

/** scrollview */
@property (nonatomic,strong) UIScrollView *scrollView;
/** 详细 */
@property (nonatomic,strong) UILabel *titleLabel;
/**是否显示两行*/
@property (nonatomic, assign) BOOL isHaveTwo;
/** topView */
@property (nonatomic,strong) UIView *topView;
/** subView */
@property (nonatomic,strong) UIView *subView;
/** line */
@property (nonatomic,strong) UILabel *topLine;
/** subLabel */
@property (nonatomic,strong) UILabel *subLabel;
/** subLine */
@property (nonatomic,strong) UILabel *subLine;
/** 购买数量 */
@property (nonatomic,strong) UILabel *numLable;
/** numLine */
@property (nonatomic,strong) UILabel *numLine;
/** rule */
@property (nonatomic,strong) UILabel *ruleLabel;
/** ruleLine */
@property (nonatomic,strong) UILabel *ruleLine;
/** payLabel */
@property (nonatomic,strong) UILabel *payLabel;
/** addBtn */
@property (nonatomic,strong) UIButton *addBtn;
/** reduceBtn */
@property (nonatomic,strong) UIButton *reduceBtn;
/** numCountLabel */
@property (nonatomic,strong) UILabel *numCountLabel;
/** cmiRule */
@property (nonatomic,strong) CMIAdrressView *ruleView;
/** 实付金额 */
@property (nonatomic,strong) UILabel *moneyLabel;


/** 双层数组 */
@property (nonatomic,strong) NSArray *dataSource;
/**第二层数组长度*/
@property (nonatomic, assign) NSInteger subCount;
/** 选中按钮 */
@property (nonatomic,strong) UIButton *currentBtn;
@end

@implementation InfoMainView

- (instancetype)init
{
    if (self = [super init]) {
        [self initUI];
    }
    return self;
}
- (void)initUI{
    _scrollView = [UIScrollView new];
    [self addSubview:_scrollView];
    
    _titleLabel = [UILabel new];
    _titleLabel.font = [UIFont systemFontOfSize:14.f];
    [self.scrollView addSubview:_titleLabel];
    
    _topView = [UIView new];
//    _topView.backgroundColor = [UIColor orangeColor];
    [self.scrollView addSubview:_topView];
    
    self.topLine = [UILabel new];
    self.topLine.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:244/255.0 alpha:1.0];
    [self.scrollView addSubview:self.topLine];
    
   
    [self.scrollView addSubview:self.subView];
    
    _subLabel = [UILabel new];
    _subLabel.font = [UIFont systemFontOfSize:14.f];
    _subLabel.text = @"面积大小";
    [self.scrollView addSubview:_subLabel];
    
    _subLine = [UILabel new];
    self.subLine.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:244/255.0 alpha:1.0];
    [self.scrollView addSubview:self.subLine];
    
    _numLable = [UILabel new];
    _numLable.font = [UIFont systemFontOfSize:14.f];
    _numLable.text = @"购买数量";
    [self.scrollView addSubview:_numLable];
    
    _addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_addBtn setImage:[UIImage imageNamed:@"Add_button"] forState:UIControlStateNormal];
    [_addBtn setImageEdgeInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
    [self.scrollView addSubview:_addBtn];
    
    _numCountLabel = [UILabel new];
    _numCountLabel.font = [UIFont systemFontOfSize:15.f];
    _numCountLabel.textAlignment = NSTextAlignmentCenter;
    _numCountLabel.text = @"1";
    [self.scrollView addSubview:_numCountLabel];
    
    _reduceBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_reduceBtn setImage:[UIImage imageNamed:@"Reduce_button"] forState:UIControlStateNormal];
    [_reduceBtn setImageEdgeInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
    [self.scrollView addSubview:_reduceBtn];
    
    
    _numLine = [UILabel new];
    self.numLine.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:244/255.0 alpha:1.0];
    [self.scrollView addSubview:self.numLine];
    
//    _ruleLabel = [UILabel new];
//    _ruleLabel.font = [UIFont systemFontOfSize:14.f];
//    _ruleLabel.text = @"使用规则";
//    [self.scrollView addSubview:_ruleLabel];
//    
//    _ruleLine = [UILabel new];
//    self.ruleLine.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:244/255.0 alpha:1.0];
//    [self.scrollView addSubview:self.ruleLine];
    
    _ruleView = [CMIAdrressView new];
    _ruleView.useLabel.text = @"使用规则";
    [self.scrollView addSubview:_ruleView];
    
    _payLabel = [UILabel new];
    _payLabel.font = [UIFont systemFontOfSize:14.f];
    _payLabel.text = @"需付金额";
    [self.scrollView addSubview:_payLabel];
    
    _moneyLabel = [UILabel new];
    _moneyLabel.font = [UIFont systemFontOfSize:14.f];
    _moneyLabel.text = @"50.0";
    [self.scrollView addSubview:_moneyLabel];
    
}
- (UIView *)subView
{
    if (!_subView) {
        self.subView = [UIView new];
//        _subView.backgroundColor = [UIColor purpleColor];
    }
    return _subView;
}
- (void)getInfoOfArray:(NSArray *)array state:(CMIInfoState)state
{
    self.dataSource = array;
    [self setState:state];
    
    __block UIButton *lastBtn = nil;
    
    NSInteger list = 4;
    NSInteger gap  = 10;
    
    for (NSInteger i = 0; i < array.count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.tag = 100+i;
        if (_isHaveTwo) {
            [btn setTitle:[[array objectAtIndex:i] objectForKey:@"type"] forState:0];
        }else{
            [btn setTitle:array[i] forState:0];
        }
        btn.titleLabel.font = [UIFont systemFontOfSize:14.f];
        [btn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        [btn addTarget:self action:@selector(btnWithDidTouch:) forControlEvents:UIControlEventTouchUpInside];
        
        [btn setBackgroundColor:[UIColor colorWithHue:(arc4random() % 256 / 256.0 ) saturation:( arc4random() % 128 / 256.0 ) + 0.5
                                        brightness:( arc4random() % 128 / 256.0 ) + 0.5 alpha:0.2]];
        if (i== 0) {
            btn.selected = YES;
            self.currentBtn = btn;
            if (_isHaveTwo) {
                NSArray *data = [self.dataSource[i] objectForKey:@"sub"];
                _subCount = data.count;
                [self createSubView:data];
                
                [_subView mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.height.mas_equalTo((_subCount/4+1)*40 + 10);
                }];
            }
        }
        [_topView addSubview:btn];
        
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@30);
            if (lastBtn) {
                make.width.equalTo(lastBtn.mas_width);
            }else{
                make.width.mas_equalTo(([[UIScreen mainScreen] bounds].size.width - gap*(list + 1))/list);
            }
            if (i%list == 0) {
                make.left.equalTo(_topView).offset(gap);
            }else{
                make.left.equalTo(lastBtn.mas_right).offset(gap);
            }
            if (i%list == (list - 1)) {
                make.right.equalTo(_topView).offset(-gap);
            }
            NSInteger top = (i/list + 1)*gap + (i/list*30);
            make.top.equalTo(_topView).offset(top);
            
        }];
        lastBtn = btn;
        
    }
}
- (void)getInfoMoreOfArrary:(NSArray *)array
{
    
}
#pragma mark - setter
- (void)setState:(CMIInfoState)state
{
    _state = state;
    if (state == CMIInfoStrings) {
        _isHaveTwo = YES;
    }else{
        _isHaveTwo = NO;
    }
}
- (void)setTitleName:(NSString *)titleName
{
    _titleName = titleName;
    _titleLabel.text = titleName;
}
- (void)setCount:(NSInteger)count
{
    _count = count;
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self).insets(UIEdgeInsetsZero);
    }];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.scrollView).offset(10);
        make.top.equalTo(self.scrollView).offset(10);
    }];
    [_topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.scrollView).offset(0);
        make.left.equalTo(self.scrollView).offset(0);
        make.top.equalTo(self.titleLabel.mas_bottom).offset(10);
        make.height.mas_equalTo((_count/4 + 1)* 40 + 10);
        make.width.mas_equalTo([UIScreen mainScreen].bounds.size.width);
    }];
    [_topLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.scrollView).offset(10);
        make.right.equalTo(self.scrollView).offset(-10);
        make.height.equalTo(@1);
        make.top.equalTo(_topView.mas_bottom);
    }];
    if (_isHaveTwo) {
        
        [_subLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.scrollView).offset(10);
            make.top.equalTo(_topLine.mas_bottom).offset(5);
        }];
        
        [_subView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.left.equalTo(self.scrollView);
            make.top.equalTo(_subLabel.mas_bottom).offset(5);
            
        }];
        [_subLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.scrollView).offset(10);
            make.right.equalTo(self.scrollView).offset(-10);
            make.top.equalTo(_subView.mas_bottom);
            make.height.equalTo(@1);
        }];
    }
    [_numLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.scrollView).offset(10);
        make.top.equalTo(_isHaveTwo?_subLine.mas_bottom:_topLine.mas_bottom).offset(10);
    }];
    
    [_reduceBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.scrollView).offset(-10);
        make.centerY.equalTo(_numLable.mas_centerY);
        make.height.equalTo(@40);
        make.width.equalTo(@40);
    }];
    
    [_numCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_reduceBtn.mas_left).offset(2);
        make.centerY.equalTo(_numLable.mas_centerY);
        make.width.equalTo(@30);
    }];
    
    [_addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_numCountLabel.mas_left).offset(-2);
        make.centerY.equalTo(_numLable.mas_centerY);
        make.height.equalTo(@40);
        make.width.equalTo(@40);
    }];
    
    [_numLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.scrollView).offset(10);
        make.right.equalTo(self.scrollView).offset(-10);
        make.top.equalTo(_numLable.mas_bottom).offset(15);
        make.height.equalTo(@1);
    }];
    
//    [_ruleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(self.scrollView).offset(15);
//        make.top.equalTo(_numLine.mas_bottom).offset(10);
//    }];
//    [_ruleLine mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(self.scrollView).offset(10);
//        make.right.equalTo(self.scrollView).offset(-10);
//        make.top.equalTo(_ruleLabel.mas_bottom).offset(15);
//        make.height.equalTo(@1);
//    }];
    [_ruleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.scrollView);
        make.right.equalTo(self.scrollView);
        make.top.equalTo(_numLine.mas_bottom);
        make.height.mas_equalTo(40);

    }];
    [_payLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.scrollView).offset(10);
        make.top.equalTo(_ruleView.mas_bottom).offset(10);
        make.bottom.equalTo(self.scrollView).offset(-15);
    }];
    
    [_moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_payLabel.mas_centerY);
        make.right.equalTo(self.scrollView).offset(-10);
    }];
    
}
#pragma mark - <#mark#>
- (void)btnWithDidTouch:(UIButton *)button
{
    if (self.currentBtn != button) {
        self.currentBtn.selected = NO;
        button.selected = YES;
        self.currentBtn = button;
       
    }
    if (_isHaveTwo) {
        NSArray *data = [self.dataSource[button.tag - 100] objectForKey:@"sub"];
        _subCount = data.count;
        [self createSubView:data];
        
        [_subView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo((_subCount/4+1)*40 + 10);
        }];
    }
}
#pragma mark - private
- (void)createSubView:(NSArray *)subArray
{
    for (UIButton *btn in _subView.subviews) {
        [btn removeFromSuperview];
    }
    __block UIButton *lastBtn = nil;
    
    NSInteger list = 4;
    NSInteger gap  = 10;
    
    for (NSInteger i = 0; i < subArray.count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.tag = 1000+i;
        [btn setTitle:subArray[i] forState:0];
        btn.titleLabel.font = [UIFont systemFontOfSize:14.f];
//        [btn addTarget:self action:@selector(btnWithDidTouch:) forControlEvents:UIControlEventTouchUpOutside];
        [btn setBackgroundColor:[UIColor colorWithHue:(arc4random() % 256 / 256.0 ) saturation:( arc4random() % 128 / 256.0 ) + 0.5
                                           brightness:( arc4random() % 128 / 256.0 ) + 0.5 alpha:0.2]];
        [_subView addSubview:btn];
        
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@30);
            if (lastBtn) {
                make.width.equalTo(lastBtn.mas_width);
            }else{
                make.width.mas_equalTo(([[UIScreen mainScreen] bounds].size.width - gap*(list + 1))/list);
            }
            if (i%list == 0) {
                make.left.equalTo(_subView).offset(gap);
            }else{
                make.left.equalTo(lastBtn.mas_right).offset(gap);
            }
            if (i%list == (list - 1)) {
                make.right.equalTo(_subView).offset(-gap);
            }
            NSInteger top = (i/list + 1)*gap + (i/list*30);
            make.top.equalTo(_subView).offset(top);
            
        }];
        lastBtn = btn;
        
    }
}
@end

@interface CMIAdrressView ()

/** bottomLine */
@property (nonatomic,strong) UILabel *bottomLabel;

@end
@implementation CMIAdrressView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self createView];
        
        
        
    }
    return self;
}
- (instancetype)init
{
    if (self = [super init]) {
        [self createView];
    }
    return self;
}
-(void)createView
{
    self.useLabel = [UILabel new];
    self.useLabel.font = [UIFont systemFontOfSize:14.f];
//    self.useLabel.textColor = MAIN_TEXT_COLOR;
//    self.useLabel.text  = @"使用规则";
    
    self.jumImageView = [UIImageView new];
        self.jumImageView.image = [UIImage imageNamed:@"C_list_arrow_1"];
    
    self.bottomLabel = [UILabel new];
    self.bottomLabel.alpha = 0.5;
    self.bottomLabel.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:244/255.0 alpha:1.0];
    [self addSubview:self.useLabel];
    [self addSubview:_jumImageView];
    [self addSubview:_bottomLabel];
    
    [self.useLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.left.equalTo(self).offset(10);
    }];
    
    [self.jumImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.right.equalTo(self).offset(-20);
    }];
    
    [self.bottomLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(10);
        make.right.equalTo(self).offset(-10);
        make.bottom.equalTo(self);
        make.height.equalTo(@1);
    }];
    
}
@end

